package bkone;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class addpingl
 */
@WebServlet("/addpingl")
public class addpingl extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static final String JDBC_URL = "jdbc:mysql://localhost:3309/blog?useUnicode=true&characterEncoding=utf8";
	private static final String DB_USER = "root";
	private static final String DB_PWD = "ZLWL";
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public addpingl() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.getWriter().append("Served at: addpingl").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
		String newpplneir = request.getParameter("newpplneir");
		String bkidsadd = request.getParameter("bkidsadd");
		System.out.println("newpplneir:" + newpplneir);
		System.out.println("bkidsadd:" + bkidsadd);
		try {
		    Class.forName("com.mysql.jdbc.Driver").newInstance();
	    } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
		    e.printStackTrace();
	    }
	    Connection cont14 = null;
	    PreparedStatement prestn14 = null;
	    try {
	    	cont14 = DriverManager.getConnection(JDBC_URL, DB_USER, DB_PWD);
	    	Statement stmt = cont14.createStatement();
	    	SimpleDateFormat sdfFull3 = new SimpleDateFormat("yyyy-MM-dd ");
			SimpleDateFormat sdfFull4 = new SimpleDateFormat(":mm:ss");
			Calendar cld = Calendar.getInstance();
			Date currentDate = new Date();
			System.out.println("完整的(前)：" + sdfFull3.format(currentDate));
			System.out.println("完整的(后)：" + sdfFull4.format(currentDate));
			System.out.println("当前天,时间：" + cld.get(Calendar.HOUR_OF_DAY));
			String now_time2 = sdfFull3.format(currentDate) + cld.get(Calendar.HOUR_OF_DAY) + sdfFull4.format(currentDate);
			System.out.println("当前时间：" + now_time2);
		    String sctpye = "insert into review (reviewer,revew_time,review_content,bei_id,ping_id) values(?,?,?,?,?)";
		    prestn14 = cont14.prepareStatement(sctpye);
		    prestn14.setString(1, "nimda");
		    prestn14.setString(2, now_time2);
		    prestn14.setString(3, newpplneir);
		    prestn14.setString(4, bkidsadd);
		    prestn14.setString(5, "0");
		    if (!newpplneir.equals("")) {
		    	prestn14.executeUpdate();
			}
			String sqlpll = "select count(id) total from review where bei_id = " + bkidsadd;
			ResultSet rspll = stmt.executeQuery(sqlpll);
			int setnumpll = 0;
			while (rspll.next()) {
				int getnumpll = rspll.getInt(1);
				setnumpll = getnumpll;
			}
			System.out.println("blog_pingl_num:" + setnumpll);
			PreparedStatement psthcppl;
			String sqlhc = "update blog set comment_count = " + setnumpll + " where id = ?";
			psthcppl = cont14.prepareStatement(sqlhc);
			psthcppl.setString(1, bkidsadd);
			psthcppl.executeUpdate();
			psthcppl.close();
			rspll.close();
			prestn14.close();
		    cont14.close();
            response.sendRedirect("/admin_bloggl.jsp");
	    } catch (SQLException e) {
		    e.printStackTrace();
	    }
	}

}
